<template>
  <div class="channel-edit">
    <!-- 我的频道 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        type="danger"
        plain
        round
        size="mini"
        class="edit-btn"
        @click="isEdit = !isEdit"
        >{{ isEdit ? '完成' : '编辑' }}</van-button
      >
    </van-cell>
    <van-grid :gutter="10" class="my-grid">
      <van-grid-item
        v-for="(channel, index) in mychannels"
        :key="index"
        class="grid-item"
        @click="onMyChannelClick(channel, index)"
      >
        <van-icon
          name="clear"
          slot="icon"
          v-show="isEdit && !fixChannels.includes(channel.id)"
        ></van-icon>
        <span slot="text" class="text" :class="{ active: index === active }">{{
          channel.name
        }}</span>
      </van-grid-item>
    </van-grid>
    <!-- 频道推荐 -->
    <van-cell :border="false">
      <div slot="title">频道推荐</div>
    </van-cell>
    <van-grid :gutter="10" class="recommend-grid">
      <van-grid-item
        class="grid-item"
        v-for="(channel, index) in recommendChannels"
        :key="index"
        :text="channel.name"
        icon="plus"
        @click="addChannels(channel)"
      />
    </van-grid>
  </div>
</template>

<script>
import {
  getAllChannels,
  addUserChannel,
  deleteUserChannle
} from '../../../api//channel'
import { mapState } from 'vuex'
import { setItem } from '../../../utils/storage'
export default {
  name: 'channelEdit',
  components: {},
  props: {
    mychannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      allChannels: [],
      isEdit: false,
      fixChannels: [0] // 固定频道的id值
    }
  },
  computed: {
    ...mapState(['user']),
    recommendChannels() {
      const channels = []
      this.allChannels.forEach(channel => {
        const ret = this.mychannels.find(mychannels => {
          return mychannels.id === channel.id
        })
        if (!ret) {
          channels.push(channel)
        }
      })
      return channels
    }
  },
  watch: {},
  created() {
    this.loadAllChannels()
  },
  mounted() {},
  methods: {
    async loadAllChannels() {
      try {
        const { data } = await getAllChannels()
        this.allChannels = data.data.channels
      } catch (err) {
        this.$toast('获取频道列表失败')
      }
    },
    async addChannels(channel) {
      this.mychannels.push(channel)
      //   数据持续化处理
      if (this.user) {
        try {
          // 已登录 把数据请求接口放到线上
          const { data } = await addUserChannel({
            id: channel.id,
            seq: this.mychannels.length
          })
          console.log(data)
        } catch (err) {
          this.$toast('保存失败，请稍后重试')
        }
      } else {
        //   未登录，把数据存储到本地
        setItem('TouTiao_Channels', this.mychannels)
      }
    },
    onMyChannelClick(channel, index) {
      if (this.isEdit) {
        if (this.fixChannels.includes(channel.id)) {
          return
        }
        this.mychannels.splice(index, 1)
        console.log(this.mychannels)
        if (index <= this.active) {
          this.$emit('update-active', this.active - 1, true)
        }
        // 删除频道持久化
        this.deleteChannel(channel.id)
      } else {
        this.$emit('update-active', index, false)
      }
    },
    async deleteChannel(id) {
      try {
        if (this.user) {
          // 已登录，则发起线上请求

          await deleteUserChannle(id)
        } else {
          //    未登录，将数据更新到本地覆盖
          setItem('TouTiao_Channels', this.mychannels)
        }
      } catch (err) {
        this.$toast('删除用户频道失败')
      }
    }
  }
}
</script>
<style lang="less" scoped>
.channel-edit {
  padding: 85px 0;
  .title-text {
    font-size: 32px;
    color: #333;
  }
  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 28px;
    color: #f85959;
    border: 1px solid #f85959;
  }
  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    /* background-color: #f4f5f6; */
    .van-grid-item__content {
      background-color: #f4f5f6;
      white-space: nowrap;
      .van-grid-item__icon-wrapper {
        position: unset;
      }
      .van-grid-item__text,
      .text {
        font-size: 28px;
        color: #222;
      }
      .active {
        color: red;
      }
      .van-grid-item__text {
        margin-top: 0;
      }
    }
  }
  /deep/ .recommend-grid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
      }
    }
  }
  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        font-size: 30px;
        top: -10px;
        right: -10px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }
}
</style>
